import { $http } from '../../request/index-api'
import regeneratorRuntime from '../../lib/runtime';
Page({
  data: {
    swiperData: [],
    iconNavData: [],
    floorData: []
  },
  //生命周期回调，第一次页面加载的时候回调
  onLoad: function (options) {
    //获取轮播图数据
    this.getSwiperData()
    //过去中间图标的icon导航数据
    this.getIconNavData()
    //获取楼层数据
    this.getFloor()
  },
  //获取轮播图数据
  async getSwiperData () {
    // wx.request({
    //   url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata',
    //   success: (result)=>{
    //     this.setData({
    //       swiperData:result.data.message
    //     })
    //   },
    //   fail: ()=>{}
    // });

    //优化
    const result = await $http({ url: '/home/swiperdata' })
    this.setData({
      swiperData: result
    })

  },
  //过去中间图标的icon导航数据
  async getIconNavData () {
    const result = await $http({ url: '/home/catitems' })
    this.setData({
      iconNavData: result
    })
  },
  async getFloor () {
    const result = await $http({ url: '/home/floordata' })
    const newResult = result.map((item, i) => {
      return item['product_list'].map(item => {
        const left = item.navigator_url.split('?')[0] + '/index?'
        const right = item.navigator_url.split('?')[1]
        return left + right
      })
    })
    result.forEach((item, i) => {
      item['product_list'].forEach((item,j)=>{
        item.navigator_url = newResult[i][j];
      })
    })
    this.setData({
      floorData: result
    })
  }
});